<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>博客列表</title>
    <link rel="stylesheet" href="./static/css/add.css">
</head>

<body>
    <div class="content">
        <h1>修改博客</h1>
        <table>
            <tr>
                <th>姓名</th>
                <th>地址</th>
            </tr>

            <tbody class="blogList">
                <tr>
                    <td><input id="blogName" type="text" value="${item.name}"></td>
                    <td><input id="blogAddress" type="text" value="${item.address}"></td>
                </tr>
                <tr>
                    <td><input type="button" value="保存" onclick="update()"></td>
                    <td><input type="button" value="取消" onclick="cancel()"></td>
                </tr>
            </tbody>

        </table>

    </div>

    <script src="./static/js/axios.min.js"></script>
    <script src="./static/js/jquery-3.7.1.min.js"></script>
    <script>

        const url = "http://localhost:5666"
        const id = new URLSearchParams(location.search).get("id");

        (function show() {
            axios.get(`${url}/blog/${id}`).then((res) => {
                const blog = res.data.data;
                console.log(blog);

                //jQuery
                $('#blogName').val(blog.name)
                $('#blogAddress').val(blog.address)

            })
        })();


        function update() {

            //原生js获取
            // let blogName = document.querySelector('#blogName').value
            // let blogAddress = document.querySelector('#blogAddress').value

            //jQuery获取input数据
            let blogName = $('#blogName').val()
            let blogAddress= $('#blogAddress').val()

            let obj = {
                name: blogName,
                address: blogAddress
            }

            axios.put(`${url}/blog/${id}`, obj).then((res) => {
                console.log(res);
                location.href = './List.html'
            })
        }

        function cancel() {
            if (confirm("确认返回列表页吗")) {
                location.href = './List.html'
            }
        }

    </script>
</body>

</html>